package org.lyh.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.lyh.base.util.Result;
import org.lyh.user.domain.Logininfo;
import org.lyh.user.pojo.dto.LogininfoDTO;
import org.lyh.user.pojo.dto.WechatBindDTO;
import org.lyh.user.query.LogininfoQuery;
import org.lyh.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/login")
@Api(tags = "登录管理")
public class LoginController {
    @Autowired
    public ILogininfoService logininfoService;
    /**
    * 接口：管理员或者用户登录
    * @param logininfoDTO
    * @return Result 响应给前端
    */
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result login(@RequestBody LogininfoDTO logininfoDTO){
        try {
            HashMap<String, Object> login = logininfoService.login(logininfoDTO);
            return Result.success(login);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 微信三方登录
     * @param code
     * @return
     */
    @PostMapping("/login/wechat")
    @ApiOperation(value = "微信登录")
    public Result loginWechat(@RequestBody String code){
        try {
            return logininfoService.loginWechat(code);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 微信扫码登录后绑定手机号
     * @param wechatBindDTO
     * @return
     */
    @PostMapping("/wechat/binder")
    @ApiOperation(value = "微信绑定手机号")
    public Result binder(@RequestBody WechatBindDTO wechatBindDTO){
        try {
            return logininfoService.bind(wechatBindDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}
